package com.lei.mq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lei.mq.entity.ProdEntity;
import com.lei.mq.entity.ResultEntity;
import com.lei.mq.exception.ApiException;
import com.lei.mq.exception.ApiException;
import com.lei.mq.service.OrderListService;


@RestController
@RequestMapping("/order")
public class ProducerController {
	@Autowired
	private OrderListService orderListService;

	/**
	 * 提交订单
	 * 
	 * @param prodEntity
	 * @return
	 */
	@PostMapping("/addOrder")
	public ResultEntity<String> addOrder(@RequestBody ProdEntity prodEntity) {
		orderListService.addOrder(prodEntity);
		return new ResultEntity<String>();
	}

	/**
	 * 付款
	 */
	@PutMapping("/payment")
	public ResultEntity<String> payment(@RequestParam("orderNumber") String orderNumber) {
		try {
			orderListService.payment(orderNumber);	
		} catch (ApiException e) {
			 return new ResultEntity<>(e.getCode(),e.getMessage());
		}
		
		return new ResultEntity<String>();
	}

	/**
	 * 关闭订单
	 */
	@PutMapping("/closeOrder")
	public ResultEntity<String> closeOrder(@RequestParam("orderNumber") String orderNumber) {
		try {
			orderListService.closeTheOrder(orderNumber);	
		} catch (ApiException e) {
			 return new ResultEntity<>(e.getCode(),e.getMessage());
		}
		return new ResultEntity<String>();
	}
}
